package com.gardeneaters.game_v0.autoplay;

import java.util.Vector;

import com.gardeneaters.game_v0.playingfield.Hexagon;
import com.gardeneaters.game_v0.rules.Hexagons;

public class BotHelper extends Hexagons{

	private Enemy myEnemies;
	private Clusters myClusters;
	protected Vector <Vector <int[]>> enemyHexPos;
	protected Vector <Vector <int[]>> noTeamHexPos;
	private int team;
	
	protected BotHelper(Hexagon[][] _hexagons, int _team) {
		super(_hexagons);
		myEnemies = new Enemy(_hexagons, _team); 
		myClusters = new Clusters(_hexagons, _team);
		team = _team;
	}
	
	protected void pointAtMe(int[] tempPos, int n) {
		Vector <int[]> posVector = new Vector<int[]>();
		//int[] tempPos = {i, j};
		//int team = hexagons[i][j].getTeam();
		posVector.add(tempPos);
		int[] prevPos, nextPos;
		boolean passedIt = false;
		
		for(int k=0; k<posVector.size() && k<n; k++)
		{
			tempPos = posVector.elementAt(k);
			//if(hexagons[tempPos[0]][tempPos[1]].getTeam()!=team)
				//;//return tempPos;

			for(int l =1; l<7; l++)
			{
				
				nextPos = getHexagonPos(l, tempPos[0], tempPos[1]);
				if(nextPos==null)
					continue;
				
				//System.out.println(" tempPos "+tempPos[0]+" "+tempPos[1]);
				passedIt = false;
				// check if we already passed through this Position or not
				for(int m =0; m<posVector.size(); m++)//use m<=k for a different design pointing pattern
				{
					prevPos = posVector.elementAt(m);
					if(prevPos[0] == nextPos[0] && prevPos[1] == nextPos[1])
					{
						passedIt = true;
						break;
					}
				}
				if(!passedIt)
				{
					if(hexagons[nextPos[0]][nextPos[1]].getTeam()==team)
					{
						hexagons[nextPos[0]][nextPos[1]].setPointingTo(getHexOppositeSide(l));
						posVector.add(nextPos);
					}
				}
			}
		}
		
	}
	
	protected void startPointingAtMe(int i, int j, int teamToPointBy){
		
		Vector <int[]> posVector = new Vector<int[]>();
		int[] tempPos = {i, j};
		//int team = hexagons[i][j].getTeam();
		posVector.add(tempPos);
		int[] prevPos, nextPos;
		boolean passedIt = false;
		
		for(int k=0; k<posVector.size(); k++)
		{
			tempPos = posVector.elementAt(k);
			//if(hexagons[tempPos[0]][tempPos[1]].getTeam()!=team)
				//;//return tempPos;

			for(int l =1; l<7; l++)
			{
				
				nextPos = getHexagonPos(l, tempPos[0], tempPos[1]);
				if(nextPos==null)
					continue;
				
				//System.out.println(" tempPos "+tempPos[0]+" "+tempPos[1]);
				passedIt = false;
				// check if we already passed through this Position or not
				for(int m =0; m<posVector.size(); m++)//use m<=k for a different design pointing pattern
				{
					prevPos = posVector.elementAt(m);
					if(prevPos[0] == nextPos[0] && prevPos[1] == nextPos[1])
					{
						passedIt = true;
						break;
					}
				}
				if(!passedIt)
				{
					if(hexagons[nextPos[0]][nextPos[1]].getTeam()==teamToPointBy)
					{
						hexagons[nextPos[0]][nextPos[1]].setPointingTo(getHexOppositeSide(l));
						posVector.add(nextPos);
					}
				}
			}
		}
		
	}
	
	protected int[] getNearestEnemyTeamHex(int i, int j){
		
		Vector <int[]> posVector = new Vector<int[]>();
		int[] tempPos = {i, j};
		int team = hexagons[i][j].getTeam();
		posVector.add(tempPos);
		int[] prevPos, nextPos;
		boolean passedIt = false;
		
		for(int k=0; k<posVector.size(); k++)
		{
			tempPos = posVector.elementAt(k);
			if(hexagons[tempPos[0]][tempPos[1]].getTeam()!=team)
				return tempPos;
			
			for(int l =1; l<7; l++)
			{
				
				nextPos = getHexagonPos(l, tempPos[0], tempPos[1]);
				if(nextPos==null)
					continue;
				//System.out.println(" tempPos "+tempPos[0]+" "+tempPos[1]);
				passedIt = false;
				for(int m =0; m<k; m++)// check if we already passed through this Position or not
				{
					prevPos = posVector.elementAt(m);
					if(prevPos[0] == nextPos[0] && prevPos[1] == nextPos[1])
					{
						passedIt = true;
						break;
					}
				}
				if(!passedIt)
				{
					posVector.add(nextPos);
				}
			}
			//System.out.println(k+"  vector size "+posVector.size());
		}
		
		return null;
	}
	
	protected void update() {
		myClusters.update();
		Vector <int[]> clusterCenters = myClusters.getCenters();
		myEnemies.update(clusterCenters);
		enemyHexPos = myEnemies.getEnemyHex();
		noTeamHexPos = myEnemies.getNoTeamHexPos();
		//TODO decide how many elements u want to engage at the same time and number of elements to attack each one of them with
		// first u will attack zero team Hex. then go for enemy// later on.. decide ratio of zero team and other team elements to attack according to the difficulty level
		//think about this
		//this method will be use for difficulty from 0-0.5
		// later on.. you can look at the left over elements
		// later on.. keeping in mind power takes time to reach a certain level.. so don't change arrow direction much often or 
	}
	
	
	
}
